﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Infrastructures;
using Models;

namespace SponsorshipWeb.agent
{
    public partial class audit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var user = AuthUtil.GetAuthentication();
                //if (user == null)
                //    Response.Redirect("../login.html");

                //if (user.CompanyID == 0)
                //    Response.Redirect("guide.html");

                //hfUserID.Value = user.UserID.ToString();

                //string mode = Request.QueryString["mode"];
                //if (mode == "edit")
                //{
                //    hfMode.Value = mode;
                //    BindUI();
                //}
            }
        }

        private void BindUI()
        {
            //IStudentService service = ServiceFactory.CreateInstance<IStudentService>();
            //var student = service.Get<Student>(hfUserID.Value);

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(hfUserID.Value.ToInt());

            //IUserInfoService userService = ServiceFactory.CreateInstance<IUserInfoService>();
            //var user = userService.Get<UserInfo>(hfUserID.Value);

            //txtID.Text = student.IdentNO;
            //txtAlipay.Text = student.AliPayNO;
            //txtName.Text = student.Name;
            //txtBank.Text = student.BankNO;
            //txtMail.Text = user.Email;
            //txtQQ.Text = user.QQ;
            //imgFront.Visible = true;
            //imgBack.Visible = true;
            //imgFront.Src = SetImg(lst, FileType.IDFront);
            //imgBack.Src = SetImg(lst, FileType.IDOpposite);
        }

        private string SetImg(IList<FileSystem> lst, FileType type)
        {
            var img = lst.FirstOrDefault(x => x.Fun == type);
            if (img != null)
                return img.Path;

            return string.Empty;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //IUserInfoService userService = ServiceFactory.CreateInstance<IUserInfoService>();
            //var user = userService.Get<UserInfo>(hfUserID.Value);
            //user.Email = txtMail.Text;
            //user.QQ = txtQQ.Text;

            //userService.Update(user, user.UserNO.ToString());

            //Student stu = null;

            //IStudentService service = ServiceFactory.CreateInstance<IStudentService>();

            //if (hfMode.Value == "edit")
            //    stu = service.Get<Student>(hfUserID.Value);
            //else
            //    stu = new Student();

            //stu.Name = txtName.Text;
            //stu.IdentNO = txtID.Text;
            //stu.AliPayNO = txtAlipay.Text;
            //stu.BankNO = txtBank.Text;

            //if (hfMode.Value == "edit")
            //{
            //    service.Update(stu, user.UserNO.ToString());
            //}
            //else
            //{
            //    stu.SchoolNO = user.UserNO;
            //    service.Create(stu, user.UserNO.ToString());
            //    var loginInfo = AuthUtil.GetAuthentication();
            //    loginInfo.StudentID = stu.StudentNO;
            //    AuthUtil.UpdateAuthentication(loginInfo);
            //    ClientScript.RegisterStartupScript(typeof(Page), "SetAuth", "SetAuth(" + user.UserNO + ");", true);
            //}

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && (fuIDBack.HasFile || fuIDFront.HasFile))
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(stu.StudentNO);
            //}

            //SaveFile(fuIDFront, FileType.IDFront, lst);
            //SaveFile(fuIDBack, FileType.IDOpposite, lst);

            //Response.Redirect("info.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Agent\\" + hfUserID.Value + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Agent;
            //fs.Belong = hfUserID.Value.ToInt();
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Agent/" + hfUserID.Value + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = hfUserID.Value.ToInt();

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}